package websocket.util;

import websocket.entity.dto.CkgnjcDTO;
import websocket.entity.vo.SerialPortVo;
import websocket.exception_.*;
import websocket.listener.NhZtSerialListener;

import java.util.List;

/**
 * @ClassName SerialUtil
 * @Des TODO
 * @Author lg
 * @Date 2022/7/4 11:58
 * @Version 1.0.0
 **/
public class SerialUtil {

    private final static int n3 = 256 * 256 * 256;

    private final static int n2 = 256 * 256;

    private final static int n1 = 256;

    public static void decode(byte recstr[], double x, double y, double q1x,
                              double q1y, double q2x, double q2y, double q1_jiajiao, double q2_jiajiao, List resultData) {

        String needData = DigitalTrans.printHexString(recstr);
        String[] sixteenArr = needData.split(" ");

        int i;
        double p1, p2, tanx, tany;
        if ((recstr[0] == 0xAA) && (recstr[1] == 0xA5) && (recstr[2] == 0x55)) {

            int sumx = 0;
            for (i = 0; i < 4; i++) {
                int num = translateToInt(sixteenArr[3 + i]);
                sumx = sum(i, sumx, num);
            }
            x = sumx / 10000.0;

            int sumy = 0;
            for (i = 0; i < 4; i++) {
                int num = translateToInt(sixteenArr[7 + i]);
                sumy = sum(i, sumy, num);
            }
            y = sumy / 10000.0;

            int sumq1x = 0;
            for (i = 0; i < 4; i++) {
                int num = translateToInt(sixteenArr[12 + i]);
                sumq1x = sum(i, sumq1x, num);
            }
            q1x = sumq1x / 10000.0;

            int sumq1y = 0;
            for (i = 0; i < 4; i++) {
                int num = translateToInt(sixteenArr[16 + i]);
                sumq1y = sum(i, sumq1y, num);
            }
            q1y = sumq1y / 10000.0;

            int sumq2x = 0;
            for (i = 0; i < 4; i++) {
                int num = translateToInt(sixteenArr[20 + i]);
                sumq2x = sum(i, sumq2x, num);
            }
            q2x = sumq2x / 10000.0;

            int sumq2y = 0;
            for (i = 0; i < 4; i++) {
                int num = translateToInt(sixteenArr[25 + i]);
                sumq2y = sum(i, sumq2y, num);
            }
            q2y = sumq2y / 10000.0;
            tanx = Math.tan(q1x * Math.PI / 180.0);
            tany = Math.tan(q1y * Math.PI / 180.0);
            p1 = tanx * tanx + tany * tany + tanx * tanx * tany * tany;
            q1_jiajiao = Math.atan(Math.sqrt(p1)) * 180 / Math.PI;
            tanx = Math.tan(q1x * Math.PI / 180.0);
            tany = Math.tan(q2y * Math.PI / 180.0);

            p2 = tanx * tanx + tany * tany + tanx * tanx * tany * tany;
            q2_jiajiao = Math.atan(Math.sqrt(p2)) * 180 / 3.14159265;

        }

    }

    /**
     * 16进制转换10进制
     *
     * @param i
     * @param sum
     * @param num
     * @return
     */
    private static int sum(int i, int sum, int num) {
        if (i == 0) {
            sum += num;
        } else if (i == 1) {
            sum += num * n1;
        } else if (i == 2) {
            sum += num * n2;
        } else if (i == 3) {
            sum += num * n3;
        }
        return sum;
    }


    public static void sendDataToSerial(char id, CkgnjcDTO dto, boolean dir) throws SerialPortParameterFailure, NoSuchPort, PortInUse, NotASerialPort, TooManyListeners, SerialPortOutputStreamCloseFailure, SendDataToSerialPortFailure {
        int temp, i;
        char sum = 0x00;
        byte[] chsend = new byte[13];
        chsend[0] = (byte) 0xAA;
        chsend[1] = (byte) 0xA5;
        chsend[2] = (byte) 0x55;
        chsend[3] = (byte) id;
        // 横滚指令
        Integer fwsnJd = Integer.parseInt(dto.getNhsnGzms());
        String jd = translateToString(fwsnJd);
        Integer fwsnSd = Integer.parseInt(dto.getNhsnSd());
        String sd = translateToString(fwsnSd);


        if (String.valueOf(id).equals("11") || String.valueOf(id).equals("12")) {
            if (jd.length() == 4) {
                chsend[4] = (byte) jd.charAt(0);
                chsend[5] = (byte) jd.charAt(1);
                chsend[6] = (byte) jd.charAt(2);
                chsend[7] = (byte) jd.charAt(3);
            }
            // 第九个字节 暂时写死
            chsend[8] = (byte) 0x00;
            if(sd.length() == 4){
                chsend[9] = (byte) sd.charAt(0);
                chsend[10] = (byte) sd.charAt(1);
                chsend[11] = (byte) sd.charAt(2);
                chsend[12] = (byte) sd.charAt(3);
            }
        } else if (String.valueOf(id).equals("1E")) {
            chsend[4] = (byte) 0x00;
            chsend[5] = (byte) 0x00;
            chsend[6] = (byte) 0x00;
            chsend[7] = (byte) 0x00;
            // 第九个字节 暂时写死
            chsend[8] = (byte) 0x00;
        }


        chsend[13] = (byte)0;
        for (i = 0; i < 13; i++) {
            sum += chsend[i];
        }
        chsend[13] = (byte) sum;
        SerialPortVo serialPortVo = SerialTool.openPort("我是zt的串口", dto.getCkh(), Integer.valueOf(dto.getBtl()));
        serialPortVo.sendData(chsend);
        serialPortVo.bindListener(new NhZtSerialListener(), 500);
    }


    /**
     * 字符串类型的字节转数字
     *
     * @param cc 带转换字节字符串
     * @return
     */
    private static int translateToInt(String cc) {
        return Integer.parseInt(DigitalTrans.covert16Zhuan10(cc));
    }

    /**
     * 数字类型转换为字符串的字节转
     *
     * @param cc 带转换数字
     * @return
     */
    private static String translateToString(int cc) {
        return DigitalTrans.algorismToHEXString(cc);
    }


}
